package org.bright.controller;

import com.github.pagehelper.PageInfo;
import org.bright.entity.Employees;
import org.bright.entity.R;
import org.bright.enums.HttpEnum;
import org.bright.mapper.EmployeesMapper;
import org.bright.service.IEmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bright
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/employees")
@CrossOrigin
public class EmployeesController {
    @Autowired
    private IEmployeesService employeeService;

    @PostMapping("/insertInto")
    public R insertEmployees(@Valid Employees employees){
        List<Employees> employeesAll = new ArrayList<>();
        employeesAll.add(employees);
        int i = employeeService.insertBatch(employeesAll);
        if (i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"新增失败");
        }
        System.out.println(i);
        return R.success(i);
    }
    @GetMapping("/selectAllByEmpNameAndPhoneNumber")
    public R<PageInfo<Employees>> selectAllByEmpNameAndPhoneNumber(@RequestParam("pageNum") Integer pageNum,@RequestParam("empName") String empName,@RequestParam("phoneNumber") String phoneNumber){
        PageInfo<Employees> employeesPageInfo = employeeService.selectAllByEmpNameAndPhoneNumber(pageNum,empName, phoneNumber);
        return R.success(employeesPageInfo);
    }
    @PostMapping("/updateEmployee")
    public R updateEmployee(@Valid Employees employees){
        int i = employeeService.updateSelective(employees);
        if(i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"修改失败");
        }
        return R.success("修改成功");
    }
    @GetMapping("/deleteByEmployeeId")
    public R deleteByEmployeeId(@RequestParam("id") Integer employeeId){
        int i = employeeService.deleteByEmployeeId(employeeId);
        if(i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"删除失败");
        }
        return R.success("删除成功");
    }
    @GetMapping("/selectAllByEmployeeId")
    public R selectAllByEmployeeId(@RequestParam("id") Integer employeeId){
        Employees employees = employeeService.selectAllByEmployeeId(employeeId);
        return R.success(employees);
    }
}
